// pages/mystudy/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    timestamp:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {  
    wx.showLoading({
      title: '加载中',
    })  
    //获取订单
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/user/getorders',
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {        
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        this.setData({
          orders: res.data.data,
          timestamp: timestamp
        })        
      }
    })
    
    wx.hideLoading()
    
  },
  toPayMent: function (e) {    
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/user/pay',
      data: {
        id: e.currentTarget.dataset.orderid,        
      },
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {         
        if (res.data.code) {      
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp.toString(),
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': 'MD5',
            'paySign': res.data.data.paySign,
            success(res) {
              console.log(res)
              wx.switchTab({
                url: '/pages/mystudy/index',
              })
            },
            fail(res) {
              console.log(res)
            }
          })
        }else{
          wx.showToast({
            icon:'none',
            title: res.data.msg,            
            duration: 2000        
          })
        }
      }
    })
  },
  onShow: function(){
    if (!app.globalData.userInfo) {
      wx.showModal({
        title: '提示',
        content: '你还没登录，请登录后再报名！',
        success(res) {
          if (res.confirm) {
            app.check();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }
    this.onLoad();
  },
  onPullDownRefresh: function () {
    this.onLoad();
    wx.stopPullDownRefresh()
    wx.hideLoading();
  }
})